package cn.jeeweb.web.modules.equipment.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.TimeUtils;
import cn.jeeweb.web.modules.equipment.entity.EqsCheck;
import cn.jeeweb.web.modules.equipment.entity.EqsFixture;
import cn.jeeweb.web.modules.equipment.mapper.EqsCheckMapper;
import cn.jeeweb.web.modules.equipment.service.IEqsCheckService;
import cn.jeeweb.web.modules.equipment.service.IEqsFixtureService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description: eqs_check服务接口
*/
@Transactional
@Service("eqsCheckService")
public class EqsCheckServiceImpl extends CommonServiceImpl<EqsCheckMapper,EqsCheck> implements IEqsCheckService {

    @Autowired
    IEqsFixtureService eqsFixtureService;

    @Override
    public boolean createCheck(String[] ids,String endTime, String json) {

        EntityWrapper<EqsFixture> entityWrapper = new EntityWrapper<>(EqsFixture.class);
        if(ids != null && ids.length > 0){
            entityWrapper.in("id",ids);
        }
        List<EqsFixture> eqsFixtures = eqsFixtureService.selectList(entityWrapper);
        List<EqsCheck> checkList = new ArrayList<EqsCheck>();
        if(eqsFixtures != null && eqsFixtures.size() > 0){
            for(EqsFixture eqsFixture : eqsFixtures){
                String freqUnit = eqsFixture.getCheckFreq();
                Date endDate = TimeUtils.getDateFromTime("yyyy-MM-dd",endTime);
                Date checkDate = new Date();
                EqsCheck eqsCheck_ = new EqsCheck();
                eqsCheck_.setName(eqsFixture.getName());
                eqsCheck_.setCode(eqsFixture.getCode());
                eqsCheck_.setCheckContent(eqsFixture.getCheckContent());
                eqsCheck_.setCheckDate(checkDate);
                checkList.add(eqsCheck_);
                while (true){
                    EqsCheck eqsCheck = new EqsCheck();
                    eqsCheck.setName(eqsFixture.getName());
                    eqsCheck.setCode(eqsFixture.getCode());
                    eqsCheck.setCheckContent(eqsFixture.getCheckContent());
                    if("one_week".equals(freqUnit)){
                        checkDate = TimeUtils.getTimeDayAfterV3("yyyy-MM-dd",checkDate,7);
                    }else if("two_week".equals(freqUnit)){
                        checkDate = TimeUtils.getTimeDayAfterV3("yyyy-MM-dd",checkDate,14);
                    }else if("three_week".equals(freqUnit)){
                        checkDate = TimeUtils.getTimeDayAfterV3("yyyy-MM-dd",checkDate,21);
                    }else if("month".equals(freqUnit)){
                        checkDate = TimeUtils.getTimeDayAfterV3("yyyy-MM-dd",checkDate,30);
                    }
                    if(checkDate.getTime() <= endDate.getTime()){
                        eqsCheck.setCheckDate(checkDate);
                        checkList.add(eqsCheck);
                    }else{
                        break;
                    }
                }
            }
        }

        EntityWrapper<EqsCheck> eqsCheckEntityWrapper = new EntityWrapper<>(EqsCheck.class);
        eqsCheckEntityWrapper.eq("code",eqsFixtures.size() == 0 ? "":eqsFixtures.get(0).getCode());
        List<EqsCheck> oldChecks = selectList(eqsCheckEntityWrapper);
        if(checkList.size() > 0){
            if(oldChecks.size() > 0){
                oldChecks.forEach(t ->{
                    checkList.removeIf(r -> r.getCode().equals(t.getCode()));
//                    && r.getCheckDate().getTime() == t.getCheckDate().getTime()
                });
            }
        }
        if(checkList.size() > 0){
            for(EqsCheck check : checkList){
                insert(check);
            }
        }
        return true;
    }
}